﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.IO;
using System.Reflection;


namespace SNPAD
{
    public class ConfigurationKeys
    {
        

      


        /// <summary>
        /// 连接字符串
        /// </summary>
        public const string ConnectionString = "ConnectionString";
         

        /// <summary>
        /// OleDb 数据库服务器名
        /// </summary>
        public const string OleDb_DbServer = "OleDb_DbServer";

        /// <summary>
        /// OleDb 数据库服务端口
        /// </summary>
        public const string OleDb_DbPort = "OleDb_DbPort";

        /// <summary>
        /// OleDb 数据库名
        /// </summary>
        public const string OleDb_DbName = "OleDb_DbName";

        /// <summary>
        /// OleDb 数据库登录用户
        /// </summary>
        public const string OleDb_DbUser = "OleDb_DbUser";


        /// <summary>
        /// OleDb 数据库登录密码
        /// </summary>
        public const string OleDb_DbPassord = "OleDb_DbPassord";

        /// <summary>
        /// SAP 名称
        /// </summary>
        public const string SAP_Name = "SAP_Name";


        /// <summary>
        /// SAP 服务器地址
        /// </summary>
        public const string SAP_AppServerHost = "SAP_AppServerHost";

        /// <summary>
        /// SAP SystemNumber
        /// </summary>
        public const string SAP_SystemNumber = "SAP_SystemNumber";

        /// <summary>
        /// SAP SAP_SystemID
        /// </summary>
        public const string SAP_SystemID = "SAP_SystemID";


        /// <summary>
        /// SAP SAP_User
        /// </summary>
        public const string SAP_User = "SAP_User";

        /// <summary>
        /// SAP SAP_Password
        /// </summary>
        public const string SAP_Password = "SAP_Password";

        /// <summary>
        /// SAP SAP_Client
        /// </summary>
        public const string SAP_Client = "SAP_Client";


        /// <summary>
        ///蓝牙序列号前缀
        /// </summary>
        public const string BlueSN_Prefix = "BlueSN_Prefix";


    }

    class Configuration
    {
        //public static NLog.Logger logger = NLog.LogManager.GetLogger("Configuration");

        Hashtable list = new Hashtable();

        private static readonly Configuration instance = new Configuration();
        public static Configuration Instance
        {
            get
            {
                return instance;
            }
        }

        private bool autoSave = false;
        public bool AutoSave
        {
            get { return autoSave; }
            set { autoSave = value; }
        }

        string filePath;
        public string FilePath
        {
            get { return filePath; }
            set { filePath = value; }
        }

        private Configuration()
        {
            filePath = GetFilePath();
            SetFileAttribute(filePath);

            Load();
        }
        #region 去掉文件只读属性
        /// <summary>
        /// 去掉文件只读属性
        /// </summary>
        /// 
        /// <param name="fullFileName"></param>
        public static void SetFileAttribute(string fullFileName)
        {
            if (File.Exists(fullFileName))
            {
               
                FileInfo fileInfo = new FileInfo(fullFileName);
                if (fileInfo.Attributes.ToString().IndexOf("ReadOnly") > -1)
                    fileInfo.Attributes = FileAttributes.Normal;
                
            }
          
        }
        #endregion

        public void SetValue(string key, object value)
        {
            // update internal list
            list[key] = value;

            // update settings file
            if (autoSave)
            {
                Save();
            }
        }

        /// <summary>
        /// Return specified settings as string.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetString(string key)
        {
            object result = null;
            lock (list.SyncRoot)
            {
                result = list[key];
            }
            return (result == null) ? String.Empty : result.ToString();
        }

        /// <summary>
        /// Return specified settings as integer.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int GetInt(string key)
        {
            string result = GetString(key);
            return (result == String.Empty) ? 0 : Convert.ToInt32(result);
        }

        /// <summary>
        /// Return specified settings as boolean.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool GetBool(string key)
        {
            string result = GetString(key);
            return (result == String.Empty) ? false : Convert.ToBoolean(result);
        }

        public void Load()
        {
            XmlTextReader reader = null;
            try
            {
                list.Clear();

                reader = new XmlTextReader(filePath);
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "add"))
                        list[reader.GetAttribute("key")] = reader.GetAttribute("value");
                }
            }
            catch (Exception )
            {
                //logger.ErrorException("Load()", ex);
            }
            finally
            {
                if (reader.ReadState != ReadState.Closed)
                {
                    reader.Close();
                }
            }
        }

        public void Save()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);

                XmlNodeList nodeList = doc.SelectNodes("/configuration/appSettings/add");
                foreach (XmlNode node in nodeList)
                {
                    node.Attributes["value"].Value = GetString(node.Attributes["key"].Value);
                }

                doc.Save(filePath);

            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private string GetFilePath()
        {
            return System.Windows.Forms.Application.ExecutablePath + ".config";
        }
    }
}
